import { IsEnum, IsString, IsOptional, IsBoolean, IsNumber, IsObject, ValidateNested, IsArray } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProvider, TargetProvider } from '../interfaces/api-adapter.interface';

/**
 * 多 API 配置管理 DTO
 */

export class ApiRouteConfigDto {
  @IsEnum(ApiProvider)
  provider: ApiProvider;

  @IsEnum(TargetProvider)
  target: TargetProvider;

  @IsBoolean()
  @IsOptional()
  enabled?: boolean = true;

  @IsString()
  @IsOptional()
  description?: string;

  @IsObject()
  @IsOptional()
  config?: Record<string, any>;
}

export class MultiApiConfigDto {
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ApiRouteConfigDto)
  routes: ApiRouteConfigDto[];

  @IsString()
  @IsOptional()
  defaultProvider?: ApiProvider;

  @IsString()
  @IsOptional()
  defaultTarget?: TargetProvider;

  @IsBoolean()
  @IsOptional()
  enableBatchProcessing?: boolean = false;

  @IsNumber()
  @IsOptional()
  maxConcurrentRequests?: number = 10;

  @IsObject()
  @IsOptional()
  globalConfig?: Record<string, any>;
}

export class UpdateApiRouteDto {
  @IsEnum(ApiProvider)
  provider: ApiProvider;

  @IsEnum(TargetProvider)
  target: TargetProvider;

  @IsBoolean()
  @IsOptional()
  enabled?: boolean;

  @IsObject()
  @IsOptional()
  config?: Record<string, any>;
}

export class ApiRoutesResponseDto {
  @IsArray()
  routes: Array<{
    provider: ApiProvider;
    target: TargetProvider;
    enabled: boolean;
    description: string;
    healthStatus: 'healthy' | 'degraded' | 'unhealthy';
    lastCheck: Date;
    config?: Record<string, any>;
  }>;

  @IsNumber()
  totalRoutes: number;

  @IsNumber()
  activeRoutes: number;

  @IsNumber()
  healthyRoutes: number;
}

export class BatchForwardRequestDto {
  @IsEnum(ApiProvider)
  provider: ApiProvider;

  @IsEnum(TargetProvider)
  target: TargetProvider;

  @IsArray()
  requests: any[];

  @IsObject()
  @IsOptional()
  config?: Record<string, any>;
}

export class BatchForwardResponseDto {
  @IsArray()
  results: Array<{
    success: boolean;
    response?: any;
    error?: string;
    duration: number;
  }>;

  @IsNumber()
  totalRequests: number;

  @IsNumber()
  successfulRequests: number;

  @IsNumber()
  failedRequests: number;

  @IsNumber()
  totalDuration: number;

  @IsNumber()
  averageDuration: number;
}